package org.liveboardgames.agricola.access.hibernate;

import org.liveboardgames.agricola.access.IGameDao;
import org.liveboardgames.agricola.domain.Game;
import org.liveboardgames.agricola.model.structure.GameLoadVisitor;
import org.liveboardgames.agricola.model.structure.Visitor;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class GameHibernateDao extends HibernateDaoSupport implements IGameDao {

  public void save(Game game) {
    // TODO: possibly handle this asynchronously
    // TODO: move all of this in the cache
    getHibernateTemplate().saveOrUpdate(game);
    Visitor visitor = new GameLoadVisitor();
    game.acceptVisit(visitor);
  }

  public Game load(Long id) {
    Game game = (Game)getHibernateTemplate().load(Game.class, id);
    Visitor visitor = new GameLoadVisitor();
    game.acceptVisit(visitor);

    return game;
  }
}
